/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Engine2D.graphics.particle;

import Engine2D.graphics.GLColour;
import Engine2D.graphics.GLImage;
import Engine2D.graphics.GLSpriteSheet;
import java.util.Random;


/**
 *
 * @author Garys
 */
public class GLParticleSystem {

    private Random random = new Random();
    private GLParticle[] particles;
    private GLColour colour; 
    private int numberOfParticles;
    private long maxLifeTime;
    private long minLifeTime;
    private int x;
    private int y;
    private float maxV;
    private float minV;
    private boolean isDead;
    private long startTime;
    private String type;
    private final float alphaStep;

    /**
     * 
     * @param x
     * @param y
     * @param numberOfPaticles
     * @param maxLifeTime
     * @param minLifeTime
     * @param maxV
     * @param minV
     * @param alphaStep
     * @param colour
     * @param type
     */
    public GLParticleSystem(int x, int y, int numberOfPaticles, long maxLifeTime, long minLifeTime, float maxV, float minV, float alphaStep, GLColour colour, String type) {
        particles = new GLParticle[numberOfPaticles];
        this.x = x;
        this.y = y;
        this.numberOfParticles = numberOfPaticles;
        this.maxLifeTime = maxLifeTime;
        this.maxLifeTime = minLifeTime;
        this.maxV = maxV;
        this.minV = minV;
        this.colour = colour;
        this.type = type;
        particles = new GLParticle[numberOfPaticles];
        this.alphaStep = alphaStep;
        
    }

    /**
     * 
     */
    public void init() {
        //GLSpriteSheet particleImage = new GLSpriteSheet(8, 8); 
        for (int i = 0; i < numberOfParticles; i++) {
            particles[i] = new GLParticle(type);
            particles[i].init(x, y, minV + (random.nextFloat() * ((maxV - minV) + 1)), minV + (random.nextFloat() * ((maxV - minV) + 1)), 1f, alphaStep, minLifeTime + (long)(Math.random() * ((maxLifeTime - minLifeTime) + 1)),System.currentTimeMillis(), colour);
            
        }

        startTime = System.currentTimeMillis();
        isDead = false;
    }

    /**
     * 
     */
    public void tick() {
        if (System.currentTimeMillis() - startTime > maxLifeTime) {
            die();
        } else {
            for (int i = 0; i < numberOfParticles; i++) {
                if (!particles[i].isDead()) {
                    particles[i].tick();
                } 
            }
        }
    }

    /**
     * 
     * @param xScroll
     * @param yScroll
     */
    public void render(int xScroll, int yScroll) {
        if (!isDead) {
            for (int i = 0; i < numberOfParticles; i++) {
                particles[i].render(xScroll, yScroll);
            }
        } 
    }

    private void die() {
        isDead = true;
    }

    /**
     * 
     * @return
     */
    public boolean IsDead() {
        return isDead;
    }
}
